home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gp_wsync.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  5.6 KB  |  209 lines

  1. /* Copyright (C) 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gp_wsync.c,v 1.2 2000/09/19 19:00:25 lpd Exp $ */
  20. /* MS Windows (Win32) thread / semaphore / monitor implementation */
  21. /* original multi-threading code by John Desrosiers */
  22. #include "malloc_.h"
  23. #include "gserror.h"
  24. #include "gserrors.h"
  25. #include "gpsync.h"
  26. #include "windows_.h"
  27. #include <process.h>
  28.  
  29. /* ------- Synchronization primitives -------- */
  30.  
  31. /* Semaphore supports wait/signal semantics */
  32.  
  33. typedef struct win32_semaphore_s {
  34.     HANDLE handle;        /* returned from CreateSemaphore */
  35. } win32_semaphore;
  36.  
  37. uint
  38. gp_semaphore_sizeof(void)
  39. {
  40.     return sizeof(win32_semaphore);
  41. }
  42.  
  43. int    /* if sema <> 0 rets -ve error, 0 ok; if sema == 0, 0 movable, 1 fixed */
  44. gp_semaphore_open(
  45.           gp_semaphore * sema    /* create semaphore here */
  46. )
  47. {
  48.     win32_semaphore *const winSema = (win32_semaphore *)sema;
  49.  
  50.     if (winSema) {
  51.     winSema->handle = CreateSemaphore(NULL, 0, max_int, NULL);
  52.     return
  53.         (winSema->handle != NULL ? 0 :
  54.          gs_note_error(gs_error_unknownerror));
  55.     } else
  56.     return 0;        /* Win32 semaphores handles may be moved */
  57. }
  58.  
  59. int
  60. gp_semaphore_close(
  61.            gp_semaphore * sema    /* semaphore to affect */
  62. )
  63. {
  64.     win32_semaphore *const winSema = (win32_semaphore *)sema;
  65.  
  66.     if (winSema->handle != NULL)
  67.     CloseHandle(winSema->handle);
  68.     winSema->handle = NULL;
  69.     return 0;
  70. }
  71.  
  72. int                /* rets 0 ok, -ve error */
  73. gp_semaphore_wait(
  74.           gp_semaphore * sema    /* semaphore to affect */
  75. )
  76. {
  77.     win32_semaphore *const winSema = (win32_semaphore *)sema;
  78.  
  79.     return
  80.     (WaitForSingleObject(winSema->handle, INFINITE) == WAIT_OBJECT_0
  81.      ? 0 : gs_error_unknownerror);
  82. }
  83.  
  84. int                /* rets 0 ok, -ve error */
  85. gp_semaphore_signal(
  86.             gp_semaphore * sema    /* semaphore to affect */
  87. )
  88. {
  89.     win32_semaphore *const winSema = (win32_semaphore *)sema;
  90.  
  91.     return
  92.     (ReleaseSemaphore(winSema->handle, 1, NULL) ? 0 :
  93.      gs_error_unknownerror);
  94. }
  95.  
  96.  
  97. /* Monitor supports enter/leave semantics */
  98.  
  99. typedef struct win32_monitor_s {
  100.     CRITICAL_SECTION lock;    /* critical section lock */
  101. } win32_monitor;
  102.  
  103. uint
  104. gp_monitor_sizeof(void)
  105. {
  106.     return sizeof(win32_monitor);
  107. }
  108.  
  109. int    /* if sema <> 0 rets -ve error, 0 ok; if sema == 0, 0 movable, 1 fixed */
  110. gp_monitor_open(
  111.         gp_monitor * mon    /* create monitor here */
  112. )
  113. {
  114.     win32_monitor *const winMon = (win32_monitor *)mon;
  115.  
  116.     if (mon) {
  117.     InitializeCriticalSection(&winMon->lock);    /* returns no status */
  118.     return 0;
  119.     } else
  120.     return 1;        /* Win32 critical sections mutsn't be moved */
  121. }
  122.  
  123. int
  124. gp_monitor_close(
  125.          gp_monitor * mon    /* monitor to affect */
  126. )
  127. {
  128.     win32_monitor *const winMon = (win32_monitor *)mon;
  129.  
  130.     DeleteCriticalSection(&winMon->lock);    /* rets no status */
  131.     return 0;
  132. }
  133.  
  134. int                /* rets 0 ok, -ve error */
  135. gp_monitor_enter(
  136.          gp_monitor * mon    /* monitor to affect */
  137. )
  138. {
  139.     win32_monitor *const winMon = (win32_monitor *)mon;
  140.  
  141.     EnterCriticalSection(&winMon->lock);    /* rets no status */
  142.     return 0;
  143. }
  144.  
  145. int                /* rets 0 ok, -ve error */
  146. gp_monitor_leave(
  147.          gp_monitor * mon    /* monitor to affect */
  148. )
  149. {
  150.     win32_monitor *const winMon = (win32_monitor *)mon;
  151.  
  152.     LeaveCriticalSection(&winMon->lock);    /* rets no status */
  153.     return 0;
  154. }
  155.  
  156. /* --------- Thread primitives ---------- */
  157.  
  158. typedef struct gp_thread_creation_closure_s {
  159.     gp_thread_creation_callback_t function;    /* function to start */
  160.     void *data;            /* magic data to pass to thread */
  161. } gp_thread_creation_closure;
  162.  
  163. /* Origin of new threads started by gp_create_thread */
  164. private void
  165. gp_thread_begin_wrapper(
  166.             void *thread_data    /* gp_thread_creation_closure passed as magic data */
  167. )
  168. {
  169.     gp_thread_creation_closure closure;
  170.  
  171.     closure = *(gp_thread_creation_closure *)thread_data;
  172.     free(thread_data);
  173.     (*closure.function)(closure.data);
  174.     _endthread();
  175. }
  176.  
  177. /* Call a function on a brand new thread */
  178. int                /* 0 ok, -ve error */
  179. gp_create_thread(
  180.          gp_thread_creation_callback_t function,    /* function to start */
  181.          void *data    /* magic data to pass to thread fn */
  182. )
  183. {
  184.     /* Create the magic closure that thread_wrapper gets passed */
  185.     gp_thread_creation_closure *closure =
  186.     (gp_thread_creation_closure *)malloc(sizeof(*closure));
  187.  
  188.     if (!closure)
  189.     return gs_error_VMerror;
  190.     closure->function = function;
  191.     closure->data = data;
  192.  
  193.     /*
  194.      * Start thread_wrapper.  The Watcom _beginthread returns (int)(-1) if
  195.      * the call fails.  The Microsoft _beginthread returns -1 (according to
  196.      * the doc, even though the return type is "unsigned long" !!!) if the
  197.      * call fails; we aren't sure what the Borland _beginthread returns.
  198.      * The hack with ~ avoids a source code commitment as to whether the
  199.      * return type is [u]int or [u]long.
  200.      *
  201.      * BEGIN_THREAD is a macro (defined in windows_.h) because _beginthread
  202.      * takes different arguments in Watcom C.
  203.      */
  204.     if (~BEGIN_THREAD(gp_thread_begin_wrapper, 0, closure) != 0)
  205.     return 0;
  206.     return_error(gs_error_unknownerror);
  207. }
  208.  
  209.